﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;

namespace VeryExtremeArt.Controller
{
    public class MemberController
    {
        public Member Login (string username, string password)
        {
            using (DataBaseDataContext db = new DataBaseDataContext())
            {
                return db.Members.SingleOrDefault(m => m.UserName == username && m.Password == password);
            }
        }

        public Member RegisterMember(Member member)
        {
            if (string.IsNullOrEmpty(member.Email) || string.IsNullOrEmpty(member.FirstName) || string.IsNullOrEmpty(member.LastName) || string.IsNullOrEmpty(member.Password) || string.IsNullOrEmpty(member.Email) || string.IsNullOrEmpty(member.UserName))
            {
                throw new Exception("One or more fields are empty. Make sure none of them are empty");
            }
            else
            {
                using (DataBaseDataContext db = new DataBaseDataContext())
                {
                    if (db.Members.Where(m => m.UserName == member.UserName).ToList().Count == 0)
                    {
                        if (db.Members.Where(m => m.Email == member.Email).ToList().Count == 0)
                        {
                            db.Members.InsertOnSubmit(member);
                            db.SubmitChanges();
                            return db.Members.FirstOrDefault(m => m.Email == member.Email && m.FirstName == member.FirstName && m.LastName == member.LastName && m.UserName == member.UserName);
                        }
                        else
                        {
                            throw new Exception("The Email is already used");
                        }
                    }
                    else
                    {
                        throw new Exception("The UserName is already used.");
                    }
                }
            }
        }
    }
}